"""
到文件末尾读不出来内容时 返回空字符串
一次读取一个字符,以原形式显示内容

文件过大时不建议一次read所有内容(占用内存过多), 建议循环读取1024/2048个字符或字节

文件方式打开
1 按字符/字节读取最灵活,适用于文本文件和二进制文件
2 3 4 按行读取都跟文本文件有关
"""
# 1.1 所有内容读取
file_path = "/day03/test.txt"
file_obj = open(file_path, 'r')

while True:
    data = file_obj.read()
    if not data:
        break
    print(data, end="")

file_obj.close()

# 1.2 按字符读取
file_path = "/day03/test.txt"
file_obj = open(file_path, 'r')

while True:
    data = file_obj.read(1)
    if not data:
        break
    print(data, end="")

file_obj.close()

# 2.1 按行读取
file_path = "/day03/test.txt"
file_obj = open(file_path, 'r')

while True:
    data = file_obj.readline()
    if not data:
        break
    print(data, end="")

file_obj.close()

# 2.2 按行限制字符读取
file_path = "/day03/test.txt"
file_obj = open(file_path, 'r')

while True:
    data = file_obj.readline(3)
    if not data:
        break
    print(data + "(⊙o⊙)", end="")

file_obj.close()

# 3.1 读取所有行到列表
file_path = "/day03/test.txt"
file_obj = open(file_path, 'r')

lines = file_obj.readlines()
print(lines)

file_obj.close()

# 3.2 读取前几行到列表
# 读取到指定字符数所在行
file_path = "/day03/test.txt"
file_obj = open(file_path, 'r')

lines01 = file_obj.readlines(5)
print(lines01)

file_obj.close()

# 4. 按行迭代
file_path = "/day03/test.txt"
file_obj = open(file_path, 'r')

for line in file_obj:
    print(line, end="")

file_obj.close()
